React router之exact

exact

  • exact是Route下的一条属性,一般而言,react路由会匹配所有匹配到的路由组价,exact能够使得路由的匹配更严格一些。
  • exact的值为bool型,为true是表示严格匹配,为false时为正常匹配。
  • 如在exact为true时,’/link’与’/’是不匹配的,但是在false的情况下它们又是匹配的

以下代码示例

1
2
3
<Route path='/' component={Home} />
<Route path='/page' component={Page}>
//这种情况下,如果匹配路由path='/page',那么会把Home也会展示出来。

所以我们经常添加exact来解决上述问题。

1
2
<Route exact path='/' component={Home} />
<Route path='/page' component={Page} />

综合以上得出结论如下:

path location.pathname exact matches?
/one /one/two true no
/one /one/two false yes
请我吃辣条吧~~
-------------本文结束感谢您的阅读-------------